package com.wmh.baseservice.operationaid.controller;

import com.alibaba.fastjson2.JSON;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.entity.KeyWordUsageRecordGroup;
import com.wmh.baseservice.operationaid.pojo.vo.keyWordUsageRecordGroup.KeyWordCreateGroupReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.keyWordUsageRecordGroup.KeyWordListReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.keyWordUsageRecordGroup.KeyWordUpdateGroupReqVO;
import com.wmh.baseservice.operationaid.service.KeyWordUsageRecordGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author 谢骏
 * @date 2023/7/11 11:17
 **/
@Validated
@RestController
@RequestMapping("operationAid/KeyWordUsageRecordGroup")
@Api(tags = "关键词记录分组")
@Slf4j
public class KeyWordUsageRecordGroupController {

    @Resource
    private KeyWordUsageRecordGroupService keyWordUsageRecordGroupService;

    @Resource
    private UserUtil userUtil;

    @ApiOperation("获取当前用户的分组列表")
    @PostMapping("/list")
    public String list(KeyWordListReqVO reqVO) {
        List<KeyWordUsageRecordGroup> list = keyWordUsageRecordGroupService.list(
                new LambdaQueryWrapperX<KeyWordUsageRecordGroup>()
                        .eq(KeyWordUsageRecordGroup::getAdId, userUtil.getId())
                        .likeIfPresent(KeyWordUsageRecordGroup::getName, reqVO.getName())
                        .eqIfPresent(KeyWordUsageRecordGroup::getCategoryName,reqVO.getCategoryName())
                        .select(KeyWordUsageRecordGroup::getId,
                                KeyWordUsageRecordGroup::getName,
                                KeyWordUsageRecordGroup::getCategoryName,
                                KeyWordUsageRecordGroup::getCreateTime,
                                KeyWordUsageRecordGroup::getUpdateTime)
                        .orderByDesc(KeyWordUsageRecordGroup::getCreateTime));
        return ReturnBody.success(list);
    }

    @ApiOperation("新增")
    @PostMapping("/create")
    public String create(@Valid KeyWordCreateGroupReqVO reqVO) {
        keyWordUsageRecordGroupService.create(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("更新")
    @PostMapping("/update")
    public String update(@Valid KeyWordUpdateGroupReqVO reqVO) {
        keyWordUsageRecordGroupService.update(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public String delete(
            @NotBlank(message = "字段未提交")
            @Size(min = 3, message = "字段未提交")
            String jsonIds) {
        List<Long> ids = JSON.parseArray(jsonIds, Long.class);
        keyWordUsageRecordGroupService.remove(ids);
        return ReturnBody.success();
    }
}
